package com.wakey.rtcs.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;



public class ResultMap {
	
	public final static String RESULT = "result";
	public final static String DATA = "data";
	public final static String MSG = "msg";
	//存放异常等错误信息，错误信息会弹错来
	public final static String ERROR_MSG = "errorMsg";
	//这个里面存放 form表单校验的错误，一个list，界面上显示是，在框后面加提示
	public final static String ERROR = "error";
	//用于区分 数据校验和系统异常
	public final static String EXCEPTION = "exception";
	
	public static String SUCCESS = "success";
	
	public static String FAIL = "false";
	
	/**
	 * 返回默认成功信息以及data
	 * @return
	 */
	public static Map<String,Object> getResultMap(Object obj){
		Map<String,Object> result = new HashMap<String, Object>();
		result.put(RESULT, ResultMap.SUCCESS);		
		result.put(DATA, obj);
		return result;
	}
	/**
	 * 返回默认失败
	 * @return
	 */
	public static Map<String,Object> getResultErrorMap(String  msg){
		Map<String,Object> result = new HashMap<String, Object>();
		result.put(RESULT, ResultMap.FAIL);
		result.put(ERROR_MSG, msg);
		return result;
	}
	/**
	 * 返回默认成功信息
	 * @return
	 */
	public static Map<String,Object> getResultMap(){
		Map<String,Object> result = new HashMap<String, Object>();
		result.put(RESULT, ResultMap.SUCCESS);
		return result;
	}
	
	/**
	 * 返回默认错误信息,用于前台展示错误信息
	 * @return
	 */
	public static Map<String,Object> getResultErrorMap(){
		Map<String,Object> result = new HashMap<String, Object>();
		result.put(RESULT, ResultMap.FAIL);
		return result;
	}
	
    
	/**
	 * 解析错误信息
	 * @param binding
	 * @return
	 */
	public static Map<String,Object> analyzeError(BindingResult binding){
		Map<String,Object> resultMap = ResultMap.getResultMap();
		List<MyError> errorList = new ArrayList<MyError>();
		resultMap.put(ResultMap.RESULT, ResultMap.FAIL);
		resultMap.put(ResultMap.ERROR, errorList);
		List<ObjectError> objErrorList = binding.getAllErrors();
		
		for(ObjectError error : objErrorList){
			try {
				errorList.add(new MyError(error.getObjectName(),(String)BeanUtils.getProperty(error, "field"),error.getDefaultMessage()));
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		return resultMap;
	}
	
	public static Map<String,Object> analyzeError(List<MyError> errorList){
		Map<String,Object> resultMap = ResultMap.getResultMap();
		resultMap.put(ResultMap.RESULT, ResultMap.FAIL);
		resultMap.put(ResultMap.ERROR, errorList);
		return resultMap;
	}
	
	public static Map<String,Object> analyzeError(MyError error){
		Map<String,Object> resultMap = ResultMap.getResultMap();
		List<MyError> errorList = new ArrayList<MyError>();
		errorList.add(error);
		resultMap.put(ResultMap.RESULT, ResultMap.FAIL);
		resultMap.put(ResultMap.ERROR, errorList);
		return resultMap;
	}
	
	/**
	 * 解析错误信息
	 * @param binding
	 * @return
	 */
	public static Map<String,Object> analyzeError(Map<String,Object> resultMap,BindingResult binding){
		List<MyError> errorList = new ArrayList<MyError>();
		resultMap.put(ResultMap.RESULT, ResultMap.FAIL);
		resultMap.put(ResultMap.ERROR, errorList);
		List<ObjectError> objErrorList = binding.getAllErrors();
		
		for(ObjectError error : objErrorList){
			try {
				errorList.add(new MyError(error.getObjectName(),(String)BeanUtils.getProperty(error, "field"),error.getDefaultMessage()));
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		return resultMap;
	}
	
}
